package com.jxb.netty.network.netty.http.server;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpRequestDecoder;
import io.netty.handler.codec.http.HttpResponseDecoder;
import io.netty.handler.codec.http.HttpResponseEncoder;
import io.netty.handler.ssl.SslContext;

public class ServerHandlerInit extends ChannelInitializer<SocketChannel> {

    private final SslContext sslCtx;

    public ServerHandlerInit(SslContext sslCtx) {
        this.sslCtx = sslCtx;
    }

    @Override
    protected void initChannel(SocketChannel sc) throws Exception {
        ChannelPipeline pipeline = sc.pipeline();
        if (sslCtx != null) {
            pipeline.addLast(sslCtx.newHandler(sc.alloc()));
        }
        //应答报文编码
        pipeline.addLast("encoder",new HttpResponseEncoder());
        //请求报文解码
        pipeline.addLast("decoder",new HttpRequestDecoder());

        //聚合http为一个完整的报文
        pipeline.addLast("aggregator",new HttpObjectAggregator(10*1024*1024));

        //把应答报文进行压缩

        //业务handler
        pipeline.addLast(new BusiHandler());
    }
}
